library(hrbrthemes); library(plyr); library(dplyr); library(tidyverse)
library(tools)

source("helper_functions.R")

estimates <- bind_rows(model.outcome(hsi_american_m2, 
                                     "Americanism Beliefs (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome(hsi_party_m2, 
                                     "Partisan Identity (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome(hsi_panethnic_m2, 
                                     "Panethnic Identity (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome(hsi_immigration_m2, 
                                     "Immigration Opinion (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome(lucid_panethnic_m2, 
                                     "Panethnic Identity (L)") %>%
                        bind_cols(study = "Lucid"),
                       model.outcome(lucid_attp_m2, 
                                     "ATTP (L)") %>%
                        bind_cols(study = "Lucid"),
                       model.outcome(lucid_immigration_m2, 
                                     "Immigration Opinion (L)") %>% 
                        bind_cols(study = "Lucid"),
                       model.outcome(lucid_panethnic_m2, 
                                     "Panethnic Identity (L)") %>%
                        bind_cols(study = "CloudResearch"),
                       model.outcome(cr1_panethnic_m2, 
                                     "Panethnic Identity (CR1)") %>%
                        bind_cols(study = "CloudResearch"),                       
                       model.outcome(cr1_pkscale_m2, 
                                     "Political Knowledge (CR1)") %>% 
                        bind_cols(study = "CloudResearch"),
                       model.outcome(cr1_efficacy_m2, 
                                     "Efficacy Beliefs (CR1)") %>% 
                        bind_cols(study = "CloudResearch"),
                       model.outcome(cr1_minrep_m2, 
                                     "Minority Representation (CR1)") %>% 
                        bind_cols(study = "CloudResearch"),
                       model.outcome(cr2_pkscale_m2, 
                                     "Political Knowledge (CR2)") %>% 
                        bind_cols(study = "CloudResearch 2"),
                       model.outcome(cr2_ideology_m2, 
                                     "Ideology (CR2)") %>% 
                        bind_cols(study = "CloudResearch 2"))

estimates_disc <- bind_rows(model.outcome_disc(hsi_american_m2, 
                                     "Americanism Beliefs (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome_disc(hsi_party_m2, 
                                     "Partisan Identity (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome_disc(hsi_panethnic_m2, 
                                     "Panethnic Identity (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome_disc(hsi_immigration_m2, 
                                     "Immigration Opinion (HSI)") %>%
                        bind_cols(study = "HSI"),
                       model.outcome_disc(lucid_panethnic_m2, 
                                     "Panethnic Identity (L)") %>%
                        bind_cols(study = "Lucid"),
                       model.outcome_disc(lucid_attp_m2, 
                                     "ATTP (L)") %>%
                        bind_cols(study = "Lucid"),
                       model.outcome_disc(lucid_immigration_m2, 
                                     "Immigration Opinion (L)") %>% 
                        bind_cols(study = "Lucid"),
                       model.outcome_disc(lucid_panethnic_m2, 
                                     "Panethnic Identity (L)") %>%
                        bind_cols(study = "CloudResearch"),
                       model.outcome_disc(cr1_panethnic_m2, 
                                     "Panethnic Identity (CR1)") %>%
                        bind_cols(study = "CloudResearch"),                       
                       model.outcome_disc(cr1_pkscale_m2, 
                                     "Political Knowledge (CR1)") %>% 
                        bind_cols(study = "CloudResearch"),
                       model.outcome_disc(cr1_efficacy_m2, 
                                     "Efficacy Beliefs (CR1)") %>% 
                        bind_cols(study = "CloudResearch"),
                       model.outcome_disc(cr1_minrep_m2, 
                                     "Minority Representation (CR1)") %>% 
                        bind_cols(study = "CloudResearch"),
                       model.outcome_disc(cr2_pkscale_m2, 
                                     "Political Knowledge (CR2)") %>% 
                        bind_cols(study = "CloudResearch 2"),
                       model.outcome_disc(cr2_ideology_m2, 
                                     "Ideology (CR2)") %>% 
                        bind_cols(study = "CloudResearch 2"))

estimates$item_name <- paste(unlist(purrr::map(strsplit(estimates$levels, 
                                                        "_Cat"), 1)))

estimates %>%
 filter(model != "Partisan Identity (HSI)" &
        model != "Americanism Beliefs (HSI)" &
        model != "Immigration Opinion (HSI)" &
        model != "Political Knowledge (CR2)") -> estimates

estimates <- estimates %>%
 mutate(item_name = recode(item_name,
                           linked_e_1 = "econ. commonalities",
                           linked_e_2 = "pol. commonalities",
                           linked_e_3 = "linked fate",
                           imm_e_1 = "imm. strengthen",
                           imm_e_2 = "path to citizenship",
                           imm_e_3 = "imm. social service",
                           imm_e_4 = "minute men",
                           latattach_e_1 = "  econ. commonalities",
                           latattach_e_2 = "   pol. commonalities",
                           latattach_e_3 = "     linked fate",
                           latattach_e_4 = "latino self affect",
                           latattach_e_5 = "latino group affect",
                           minrep_e_3 = "crooked people",
                           minrep_e_4 = "Few big interests",
                           minrep_e_5 = "elections make no diff",
                           minrep_e_6 = "voting laws disc.",
                           minrep_e_7 = "gov treat latinos",
                           minrep_e_8 = "voting in US affect",
                           latino_e_1 = "importance",
                           latino_e_2 = "we vs they",
                           latino_e_3 = "think of self",
                           outcomes_e_1 = "comfort working",
                           outcomes_e_3 = "gender self-id",
                           outcomes_e_4 = "gender confirm",
                           outcomes_e_5 = "support friend",
                           outcomes_e_7 = "protect discrim.",
                           outcomes_e_8 = "bathroom belief",
                           outcomes_e_9 = "bathroom bill",
                           outcome_e_1 = "suffering migrants",
                           outcome_e_2 = "burden immigrants",
                           outcome_e_3 = "border wall",
                           racerep_e_1 = "partisan candidate",
                           racerep_e_2 = "party majority",
                           racerep_e_3 = "descriptive rep",
                           outcomes_2_e = "border wall",
                           outcomes_10_s = "suffering migrants",
                           outcomes_10_e = "burden immigrants",
                           polknow_e_1 = "VP",
                           polknow_e_2 = "speaker",
                           polknow_e_3 = "sec. of treasury",
                           polknow_e_4 = "attorney general",
                           polknow_e_5 = "majority leader",
                           polknow_e_6 = "Sonia Sotomayor",
                           polknow_e_7 = "Marco Rubio",
                           polknow_e_8 = "AOC",
                           polknow_e_9 = "Julian Castro",
                           polknow_e_10 = "Senate Majority",
                           polknow_e_11 = "State Majority",
                           polknow_e_12 = "Governor",
                           polknow_e_13 = "Senator 1",
                           polknow_e_14 = "Senator 2",
                           pk_e_1 = "biden",
                           pk_e_2 = "trump",
                           pk_e_3 = "rubio",
                           pk_e_4 = "AOC",
                           pk_e_5 = "Greene",
                           pk_e_6 = "booker",
                           pk_e_7 = "warren",
                           pk_e_8 = "sotomayor",
                           pk_e_9 = "kagan",
                           pk_e_10 = "cruz",
                           pk_e_11 = "pelosi",
                           pk_e_12 = "castro",
                           pk_e_13 = "buttigieg",
                           pk_e_14 = "grisham",
                           rollcall2_e_1 = "tpp",
                           rollcall2_e_2 = "USA freedom act",
                           rollcall2_e_3 = "trade adjustment",
                           rollcall2_e_4 = "education reform",
                           rollcall2_e_5 = "repeal aca",
                           rollcall2_e_6 = "end dadt",
                           rollcall2_e_7 = "american clean energy",
                           rollcall2_e_8 = "stem cell",
                           rollcall2_e_9 = "medicare reform",
                           rollcall2_e_10 = "north korea",
                           rollcall2_e_11 = "minimum wage",
                           rollcall2_e_12 = "comp. health reform",
                           rollcall2_e_13 = "financial reform",
                           rollcall2_e_14 = "fisa")) %>%
 mutate(item_name = toTitleCase(item_name))

dif_plot <- estimates %>% mutate(sig = factor(2*as.numeric(diff - 1.96*se > 0) +  
                                    1*as.numeric(diff + 1.96*se < 0), 
                                  c(0,1,2), 
                                  c("Not Significant", 
                                    "Easier in English (p<.05)", 
                                    "Easier in Spanish (p<.05)"))) %>% 
  group_by(item_name, study) %>%
  dplyr::mutate(cats = row_number()) %>% 
  ggplot(aes(x = forcats::fct_reorder(item_name, cats),
             y = cats, fill = sig, group = model)) +
  geom_tile(width=.75, height=.75, alpha = .65) + 
  geom_text(aes(label= round(diff, 1)), size = 4) +
  coord_flip() + 
  theme_ipsum_rc() + 
  scale_fill_manual(values=c("#F3F3F3", "#999999", "#333333")) + 
  theme(legend.position = "bottom") + labs(y = "Response category (k/k+1)") + 
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())  +
  facet_wrap(. ~ forcats::fct_reorder(model, study), 
             scales = "free",
             ncol = 3) +
  theme(axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size = 10),
        strip.text.x = element_text(size = 10),
        text = element_text(size = 10)) +
 guides(fill=guide_legend(title="")) 

dif_plot
